#include "datetimepicker.h"
#include "ui_datetimepicker.h"

DateTimePicker::DateTimePicker(QWidget *parent, int type, QString whatsThis, QDateTime dt) :
    QWidget(parent),
    ui(new Ui::DateTimePicker)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();
    connect(ui->btnYearUp, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnYearDown, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnMonthUp, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnMonthDown, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnDayUp, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnDayDown, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnHourUp, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnHourDown, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnMinuteUp, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnMinuteDown, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnSecondUp, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);
    connect(ui->btnSecondDown, &QAbstractButton::clicked, this, &DateTimePicker::turnPageSlot);

    connect(ui->btnClose, &QAbstractButton::clicked, this, &DateTimePicker::on_btnClose_clicked);
    connect(ui->btnNow, &QAbstractButton::clicked, this, &DateTimePicker::on_btnNow_clicked);
    connect(ui->btnConfirm, &QAbstractButton::clicked, this, &DateTimePicker::on_btnConfirm_clicked);

    connect(bdf, &BllDataFlow::buildFinishedSignal, this, &DateTimePicker::setWidgetParent);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &DateTimePicker::retranslateUiSlot);

    this->setWhatsThis(whatsThis);
    init(type, dt);
}

DateTimePicker::~DateTimePicker()
{
    delete ui;
}

void DateTimePicker::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void DateTimePicker::setWidgetParent()
{
    this->setParent(Project::Instance());
}

void DateTimePicker::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void DateTimePicker::init(int type, QDateTime dt)
{
    if (windowWidth >= 1280) {
        this->setGeometry(0,0,1280,800);
    }
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    this->setWindowFlags(this->windowFlags() | Qt::SubWindow);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->mainForm->setGraphicsEffect(shadow);

    for (int i = 2000;i < 2050;i++) {
        QListWidgetItem *item = new QListWidgetItem(QString::number(i));
        item->setTextAlignment(Qt::AlignCenter);
        ui->yearBox->addItem(item);
    }
    for (int i = 1;i <= 12;i++) {
        QString monthStr = i >= 10 ? QString::number(i) : ("0" + QString::number(i));
        QListWidgetItem *item = new QListWidgetItem(monthStr);
        item->setTextAlignment(Qt::AlignCenter);
        ui->monthBox->addItem(item);
    }
    for (int i = 1;i <= 31;i++) {
        QString dayStr = i >= 10 ? QString::number(i) : ("0" + QString::number(i));
        QListWidgetItem *item = new QListWidgetItem(dayStr);
        item->setTextAlignment(Qt::AlignCenter);
        ui->dayBox->addItem(item);
    }
    for (int i = 0;i < 24;i++) {
        QString hourStr = i >= 10 ? QString::number(i) : ("0" + QString::number(i));
        QListWidgetItem *item = new QListWidgetItem(hourStr);
        item->setTextAlignment(Qt::AlignCenter);
        ui->hourBox->addItem(item);
    }
    for (int i = 0;i < 60;i++) {
        QString minuteStr = i >= 10 ? QString::number(i) : ("0" + QString::number(i));
        QListWidgetItem *item = new QListWidgetItem(minuteStr);
        item->setTextAlignment(Qt::AlignCenter);
        ui->minuteBox->addItem(item);
    }
    for (int i = 0;i < 60;i++) {
        QString secondStr = i >= 10 ? QString::number(i) : ("0" + QString::number(i));
        QListWidgetItem *item = new QListWidgetItem(secondStr);
        item->setTextAlignment(Qt::AlignCenter);
        ui->secondBox->addItem(item);
    }

    dateTimeType = type;
    ui->yearOutBox->setVisible((type & DateTimeType::Year) == DateTimeType::Year);
    ui->monthOutBox->setVisible((type & DateTimeType::Month) == DateTimeType::Month);
    ui->dayOutBox->setVisible((type & DateTimeType::Day) == DateTimeType::Day);
    ui->hourOutBox->setVisible((type & DateTimeType::Hour) == DateTimeType::Hour);
    ui->minuteOutBox->setVisible((type & DateTimeType::Minute) == DateTimeType::Minute);
    ui->secondOutBox->setVisible((type & DateTimeType::Second) == DateTimeType::Second);
    if ((type & DateTimeType::Date) == 0 || (type & DateTimeType::Time) == 0) {
        ui->dateTimeSpacer->changeSize(0, 20, QSizePolicy::Fixed);
    } else {
        ui->dateTimeSpacer->changeSize(20, 20, QSizePolicy::Fixed);
    }

    setDateTime(dt);
}
void DateTimePicker::setDateTime(QDate d)
{
    year = d.year();
    month = d.month();
    day = d.day();
    hour = 0;
    minute = 0;
    second = 0;
    QTime t = QTime(hour, minute, second);
    dateTime = QDateTime(d, t);
    updateUi();
}
void DateTimePicker::setDateTime(QTime t)
{
    year = 2000;
    month = 1;
    day = 1;
    hour = t.hour();
    minute = t.minute();
    second = t.second();
    QDate d = QDate(year, month, day);
    dateTime = QDateTime(d, t);
    updateUi();
}
void DateTimePicker::setDateTime(QDateTime dt)
{
    dateTime = dt;
    QDate date = dateTime.date();
    QTime time = dateTime.time();
    year = date.year();
    month = date.month();
    day = date.day();
    hour = time.hour();
    minute = time.minute();
    second = time.second();
    updateUi();
}

void DateTimePicker::updateUi()
{
    int index = -1;
    for (int i = 0;i < 50;i++)
    {
        if (year == i + 2000)
        {
           index = i;
           break;
        }
    }
    ui->yearBox->setCurrentRow(index);
    ui->yearBox->verticalScrollBar()->setValue(index);
    index = -1;
    for (int i = 0;i < 12;i++)
    {
        if (month == i + 1)
        {
           index = i;
           break;
        }
    }
    ui->monthBox->setCurrentRow(index);
    ui->monthBox->verticalScrollBar()->setValue(index);
    index = -1;
    for (int i = 0;i < 31;i++)
    {
        if (day == i + 1)
        {
           index = i;
           break;
        }
    }
    ui->dayBox->setCurrentRow(index);
    ui->dayBox->verticalScrollBar()->setValue(index);
    index = -1;
    for (int i = 0;i < 24;i++)
    {
        if (hour == i)
        {
           index = i;
           break;
        }
    }
    ui->hourBox->setCurrentRow(index);
    ui->hourBox->verticalScrollBar()->setValue(index);
    index = -1;
    for (int i = 0;i < 60;i++)
    {
        if (minute == i)
        {
           index = i;
           break;
        }
    }
    ui->minuteBox->setCurrentRow(index);
    ui->minuteBox->verticalScrollBar()->setValue(index);
    index = -1;
    for (int i = 0;i < 60;i++)
    {
        if (second == i)
        {
           index = i;
           break;
        }
    }
    ui->secondBox->setCurrentRow(index);
    ui->secondBox->verticalScrollBar()->setValue(index);
}

void DateTimePicker::turnPageSlot()
{
    int offset = 5;
    QAbstractButton* btn = qobject_cast<QAbstractButton*>(sender());
    if (btn->objectName() == "btnYearUp") {
        ui->yearBox->verticalScrollBar()->setValue(ui->yearBox->verticalScrollBar()->value() - offset);
    } else if (btn->objectName() == "btnYearDown") {
        ui->yearBox->verticalScrollBar()->setValue(ui->yearBox->verticalScrollBar()->value() + offset);
    } else if (btn->objectName() == "btnMonthUp") {
        ui->monthBox->verticalScrollBar()->setValue(ui->monthBox->verticalScrollBar()->value() - offset);
    } else if (btn->objectName() == "btnMonthDown") {
        ui->monthBox->verticalScrollBar()->setValue(ui->monthBox->verticalScrollBar()->value() + offset);
    } else if (btn->objectName() == "btnDayUp") {
        ui->dayBox->verticalScrollBar()->setValue(ui->dayBox->verticalScrollBar()->value() - offset);
    } else if (btn->objectName() == "btnDayDown") {
        ui->dayBox->verticalScrollBar()->setValue(ui->dayBox->verticalScrollBar()->value() + offset);
    } else if (btn->objectName() == "btnHourUp") {
        ui->hourBox->verticalScrollBar()->setValue(ui->hourBox->verticalScrollBar()->value() - offset);
    } else if (btn->objectName() == "btnHourDown") {
        ui->hourBox->verticalScrollBar()->setValue(ui->hourBox->verticalScrollBar()->value() + offset);
    } else if (btn->objectName() == "btnMinuteUp") {
        ui->minuteBox->verticalScrollBar()->setValue(ui->minuteBox->verticalScrollBar()->value() - offset);
    } else if (btn->objectName() == "btnMinuteDown") {
        ui->minuteBox->verticalScrollBar()->setValue(ui->minuteBox->verticalScrollBar()->value() + offset);
    } else if (btn->objectName() == "btnSecondUp") {
        ui->secondBox->verticalScrollBar()->setValue(ui->secondBox->verticalScrollBar()->value() - offset);
    } else if (btn->objectName() == "btnSecondDown") {
        ui->secondBox->verticalScrollBar()->setValue(ui->secondBox->verticalScrollBar()->value() + offset);
    }
}

void DateTimePicker::on_btnNow_clicked()
{
    QDateTime now = QDateTime::currentDateTime();
    QDate date = now.date();
    QTime time = now.time();
    year = (dateTimeType & DateTimeType::Year) == DateTimeType::Year ? date.year() : 2000;
    month = (dateTimeType & DateTimeType::Month) == DateTimeType::Month ? date.month() : 1;
    day = (dateTimeType & DateTimeType::Day) == DateTimeType::Day ? date.day() : 1;
    hour = (dateTimeType & DateTimeType::Hour) == DateTimeType::Hour ? time.hour() : 0;
    minute = (dateTimeType & DateTimeType::Minute) == DateTimeType::Minute ? time.minute() : 0;
    second = (dateTimeType & DateTimeType::Second) == DateTimeType::Second ? time.second() : 0;
    dateTime = QDateTime(QDate(year, month, day), QTime(hour, minute, second));
    updateUi();
    bdf->selectDateTimePicker(dateTime, this->whatsThis());
    close();
}

void DateTimePicker::on_btnConfirm_clicked()
{
    if (ui->yearBox->currentItem()) {
        year = ui->yearBox->currentItem()->text().toInt();
    } else {
        year = 2000;
    }
    if (ui->monthBox->currentItem()) {
        month = ui->monthBox->currentItem()->text().toInt();
    } else {
        month = 1;
    }
    if (ui->dayBox->currentItem()) {
        day = ui->dayBox->currentItem()->text().toInt();
    } else {
        day = 1;
    }
    if (ui->hourBox->currentItem()) {
        hour = ui->hourBox->currentItem()->text().toInt();
    } else {
        hour = 0;
    }
    if (ui->minuteBox->currentItem()) {
        minute = ui->minuteBox->currentItem()->text().toInt();
    } else {
        minute = 0;
    }
    if (ui->secondBox->currentItem()) {
        second = ui->secondBox->currentItem()->text().toInt();
    } else {
        second = 0;
    }
    QDate date = QDate(year,month,day);
    QTime time = QTime(hour,minute,second);
    dateTime = QDateTime(date, time);
    updateUi();
    bdf->selectDateTimePicker(dateTime, this->whatsThis());
    close();
}

void DateTimePicker::on_monthBox_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    int m = current->text().toInt();
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
        ui->dayBox->item(28)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        ui->dayBox->item(29)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        ui->dayBox->item(30)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    } else if (m == 4 || m == 6 || m == 9 || m == 11) {
        ui->dayBox->item(28)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        ui->dayBox->item(29)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        ui->dayBox->item(30)->setFlags(Qt::NoItemFlags);
        if (ui->dayBox->currentRow() >= 30) {
            ui->dayBox->setCurrentRow(29);
        }
    } else {
        if (ui->yearBox->currentItem()) {
            int y = ui->yearBox->currentItem()->text().toInt();
            bool isLeapYear = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
            ui->dayBox->item(28)->setFlags(isLeapYear ? (Qt::ItemIsEnabled | Qt::ItemIsSelectable) : Qt::NoItemFlags);
            ui->dayBox->item(29)->setFlags(Qt::NoItemFlags);
            ui->dayBox->item(30)->setFlags(Qt::NoItemFlags);
            if (ui->dayBox->currentRow() >= (isLeapYear ? 29 : 28)) {
                ui->dayBox->setCurrentRow(isLeapYear ? 28 : 27);
            }
        }
    }
}


void DateTimePicker::on_yearBox_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (ui->monthBox->currentItem()) {
        if (ui->monthBox->currentItem()->text().toInt() == 2) {

            int y = current->text().toInt();
            bool isLeapYear = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
            ui->dayBox->item(28)->setFlags(isLeapYear ? (Qt::ItemIsEnabled | Qt::ItemIsSelectable) : Qt::NoItemFlags);
            if (ui->dayBox->currentRow() >= (isLeapYear ? 29 : 28)) {
                ui->dayBox->setCurrentRow(isLeapYear ? 28 : 27);
            }
        }
    }
}


void DateTimePicker::on_btnClose_clicked()
{
    close();
}
